package org.boulderfoodrescue.webapp.client.pickuptimes.timepicker;

import java.util.Date;

import org.boulderfoodrescue.webapp.client.old.event.ViewClosedEvent;
import org.boulderfoodrescue.webapp.client.old.event.ViewClosedHandler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

/**
 * A custom widget for creating a pickup window. 
 * 
 * @author Sean
 *
 */
public class DateTimePicker extends Composite{

	private static DateTimePickerUiBinder uiBinder = GWT
			.create(DateTimePickerUiBinder.class);

	interface DateTimePickerUiBinder extends UiBinder<Widget, DateTimePicker> {
	}

	@UiField
	DateBox start_date;
	@UiField
	TimePicker start_time;
	@UiField
	DateBox end_date;
	@UiField
	TimePicker end_time;
	@UiField
	Label time_zone;
	@UiField
	CheckBox all_day;
	@UiField
	CheckBox repeat;
	@UiField
	Label repeat_edit;

	private PickupWindow pickupWindow;

	public DateTimePicker() {
		initWidget(uiBinder.createAndBindUi(this));
		pickupWindow = new PickupWindow();
		start_date.setValue(new Date());
		end_date.setValue(new Date());
		pickupWindow.setStart_time(getStartDate());
		pickupWindow.setEnd_time(getEndDate());
		init();
	}

	public DateTimePicker(PickupWindow pickupWindow){
		initWidget(uiBinder.createAndBindUi(this));
		this.pickupWindow = pickupWindow;
		setEndDate(pickupWindow.getEnd_time());
		setStartDate(pickupWindow.getStart_time());
		init();
	}

	/**
	 * Called by the constructor
	 */
	private void init(){
		start_date.setFormat(new ShortDateFormat());
		end_date.setFormat(new ShortDateFormat());
		start_date.addValueChangeHandler(new ValueChangeHandler<Date>(){
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				pickupWindow.setStart_time(getStartDate());
			}
		});
		start_time.addValueChangeHandler(new ValueChangeHandler<Time>(){
			@Override
			public void onValueChange(ValueChangeEvent<Time> event) {
				pickupWindow.setStart_time(getStartDate());
			}
		});
		end_date.addValueChangeHandler(new ValueChangeHandler<Date>(){
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				pickupWindow.setEnd_time(getEndDate());
			}
		});
		end_time.addValueChangeHandler(new ValueChangeHandler<Time>(){
			@Override
			public void onValueChange(ValueChangeEvent<Time> event) {
				pickupWindow.setEnd_time(getEndDate());
			}
		});
	}

	/**
	 * Returns the end date
	 * 
	 * @return End Date
	 */
	private Date getEndDate(){
		if(end_date.getValue() == null || end_time.getValue() == null)
			return null;
		Date date = new Date(0);
		date.setYear(end_date.getValue().getYear());
		date.setMonth(end_date.getValue().getMonth());
		date.setDate(end_date.getValue().getDate());
		date.setHours(end_time.getValue().getHour());
		date.setMinutes(end_time.getValue().getMinute());
		return date;
	}
	private Date getStartDate(){
		if(start_date.getValue() == null || start_time.getValue() == null)
			return null;
		Date date = new Date(0);
		date.setYear(start_date.getValue().getYear());
		date.setMonth(start_date.getValue().getMonth());
		date.setDate(start_date.getValue().getDate());
		date.setHours(start_time.getValue().getHour());
		date.setMinutes(start_time.getValue().getMinute());
		return date;
	}
	private void setEndDate(Date date){
		end_date.setValue(date);
		end_time.setValue(new Time(date.getHours(), date.getMinutes()));
	}
	private void setStartDate(Date date){
		start_date.setValue(date);
		start_time.setValue(new Time(date.getHours(), date.getMinutes()));
	}
	@UiHandler ("time_zone")
	public void onTimeZoneClick(ClickEvent event){
		TimeZoneDialogBox dlg = new TimeZoneDialogBox();
		dlg.center();
	}
	@UiHandler ("all_day")
	public void onAllDayChange(ClickEvent event){
		boolean checked = ((CheckBox) event.getSource()).getValue();
		if(checked){
			start_time.setVisible(false);
			end_time.setVisible(false);
		} else{
			start_time.setVisible(true);
			end_time.setVisible(true);
		}
		pickupWindow.setAllDay(checked);
	}
	@UiHandler ("repeat")
	public void onRepeatChange(ClickEvent event){
		boolean checked = ((CheckBox) event.getSource()).getValue();
		if(checked){
			RepeatDialogBox dlg = new RepeatDialogBox(pickupWindow);
			dlg.center();
			dlg.addViewClosedHandler(new ViewClosedHandler<Boolean>(){
				@Override
				public void onViewClosed(ViewClosedEvent<Boolean> event) {
					if(event.getCloseResult())
						repeat_edit.setVisible(true);
					repeat.setValue(event.getCloseResult(), false);
				}
			});
		} else
			repeat_edit.setVisible(false);
	}

	@UiHandler ("repeat_edit")
	public void onEditClick(ClickEvent event){
		RepeatDialogBox dlg = new RepeatDialogBox(pickupWindow);
		dlg.center();
	}

	public PickupWindow getPickupWindow(){
		PickupWindow window = pickupWindow.clone();
		window.setStart_time(getStartDate());
		window.setEnd_time(getEndDate());
		return window;
	}
}
